//
//  TMSearchHeader.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/7/10.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMSearchHeader.h"
#import "SPToolSet.h"
@interface TMSearchHeader()<UITextFieldDelegate>
@property (nonatomic, assign) BOOL priceTap;
@property (nonatomic, assign) BOOL saleTap;

@end
@implementation TMSearchHeader
-(void)awakeFromNib{
    [super awakeFromNib];
    self.borderView.layer.borderWidth = 1.0f;
    self.borderView.layer.borderColor = [UIColor hx_colorWithHexRGBAString:@"EEEEEE"].CGColor;
    self.borderView.layer.cornerRadius = 5;
    self.borderView.layer.masksToBounds = YES;
    self.searchTf.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didInsert:) name:UITextFieldTextDidChangeNotification object:nil];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(allAction:)];
    [self.regoinView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bussinessAction:)];
    [self.courseview addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(priceAction:)];
    [self.prcieView addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saleAction:)];
    [self.saleView addGestureRecognizer:tap3];
    self.priceTap = NO;
    self.saleTap = NO;
}
- (void)allAction:(UITapGestureRecognizer *)taps{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ALLSEARCHNOTI" object:nil userInfo:@{@"index":@(1),@"tag":@(111)}];
}
- (void)bussinessAction:(UITapGestureRecognizer *)taps{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ALLSEARCHNOTI" object:nil userInfo:@{@"index":@(2),@"tag":@(111)}];
}
- (void)priceAction:(UITapGestureRecognizer *)taps{
    NSInteger tags;
    if (self.priceTap == YES) {
        UIButton *btn = self.priceBtns[0];
        btn.selected = YES;
        tags = btn.tag;
        UIButton *btn1 = self.priceBtns[1];
        btn1.selected = NO;
        self.priceTap = NO;
    }else{
        UIButton *btn = self.priceBtns[0];
        btn.selected = NO;
        UIButton *btn1 = self.priceBtns[1];
        tags = btn1.tag;
        btn1.selected = YES;
        self.priceTap = YES;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ALLSEARCHNOTI" object:nil userInfo:@{@"index":@(3),@"tag":@(tags)}];
}
- (void)saleAction:(UITapGestureRecognizer *)taps{
     NSInteger tags;
    if (self.priceTap == YES) {
        UIButton *btn = self.saleNumberBtns[0];
        btn.selected = YES;
        tags = btn.tag;
        UIButton *btn1 = self.saleNumberBtns[1];
        btn1.selected = NO;
        self.priceTap = NO;
    }else{
        UIButton *btn = self.saleNumberBtns[0];
        btn.selected = NO;
        UIButton *btn1 = self.saleNumberBtns[1];
        tags = btn1.tag;
        btn1.selected = YES;
        self.priceTap = YES;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ALLSEARCHNOTI" object:nil userInfo:@{@"index":@(4),@"tag":@(tags)}];
}
//ALLSEARCHNOTI BUSSINESSSEARCHNOTI PRICESEARCHNOTI SALESEARCHNOTI
//- (IBAction)priceAction:(UIButton *)sender {
//    //10 11
//    sender.selected =!sender.selected;
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"ALLSEARCHNOTI" object:nil userInfo:@{@"index":@(3),@"tag":@(sender.tag)}];
//}
//- (IBAction)saleAction:(UIButton *)sender {
//    //20 21
//    sender.selected =!sender.selected;
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"ALLSEARCHNOTI" object:nil userInfo:@{@"index":@(4),@"tag":@(sender.tag)}];
//}
- (void)didInsert:(UITextField *)textFiled
{
    textFiled = self.searchTf;
    if (self.searchTf.text.length>0) {
        self.placehoderLabel.hidden = YES;
        self.searchImage.hidden = YES;
    }else
    {
        self.placehoderLabel.hidden = NO;
        self.searchImage.hidden = NO;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([self.searchTf isFirstResponder]) {
        [self.searchTf resignFirstResponder];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BEGAINSEARCHNOTI" object:nil userInfo:@{@"text":textField.text}];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}
@end
